package gui;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import service.Service;
import model.Game;

public class Main {
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {				
		frame = new JFrame();
		frame.setBounds(100, 100, 691, 453);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 50, 368, 365);
		frame.getContentPane().add(scrollPane);
		
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		/*JPanel panel = new JPanel();
		panel.setBounds(388, 11, 285, 404);
		frame.getContentPane().add(panel);*/
		
		JButton btnRun = new JButton("Run");
		btnRun.setBounds(209, 11, 63, 23);
		frame.getContentPane().add(btnRun);
		
		JComboBox comboBox = new JComboBox(Service.getInstance().getGames().toArray());
		comboBox.setBounds(50, 11, 149, 22);
		frame.getContentPane().add(comboBox);
		
		JLabel lblGame = new JLabel("Game");
		lblGame.setBounds(12, 11, 46, 14);
		frame.getContentPane().add(lblGame);
		
		/*Object[][] properties = {
				{"Generations", 1000},
				{"Agents", 110},
				{"Initial Reputation", 0.0},
				{"Cooperation Cost", 5},
				{"Benefit", 5},
				{"History Size", 10}
				};

		int GENERATIONS = 1000;
		int NAGENTS = 110;
		double STARTINGREPUTATION = 0.0; // +- 25%.
		double COST = 5;
		double BENEFIT = 5;
		int HISTORYSIZE = 10; //0 for no history	*/
		String[] columnNames = { "Property", "Value" };
		JTable table = new JTable(((Game) comboBox.getSelectedItem()).getPropertiesArray(), columnNames) {
			private Class editingClass;

			public TableCellRenderer getCellRenderer(int row, int column) {
				editingClass = null;
				int modelColumn = convertColumnIndexToModel(column);

				if (modelColumn == 1) {
					Class rowClass = getModel()
							.getValueAt(row, modelColumn).getClass();
					return getDefaultRenderer(rowClass);
				} else
					return super.getCellRenderer(row, column);
			}

			public TableCellEditor getCellEditor(int row, int column) {
				editingClass = null;
				int modelColumn = convertColumnIndexToModel(column);

				if (modelColumn == 1) {
					editingClass = getModel().getValueAt(row, modelColumn)
							.getClass();
					return getDefaultEditor(editingClass);
				} else
					return super.getCellEditor(row, column);
			}

			// This method is also invoked by the editor when the value in
			// the editor
			// component is saved in the TableModel. The class was saved
			// when the
			// editor was invoked so the proper class can be created.

			public Class getColumnClass(int column) {
				return editingClass != null ? editingClass : super
						.getColumnClass(column);
			}
		};

		table.setPreferredScrollableViewportSize(table.getPreferredSize());

		JScrollPane scrollPaneProps = new JScrollPane(table);
		scrollPaneProps.setLocation(385, 13);
		scrollPaneProps.setSize(288, 372);
		scrollPane.setBounds(10, 50, 368, 335);
		frame.getContentPane().add(scrollPaneProps);
		
		JPanel panelProps = new JPanel();
		panelProps.setBounds(388, 11, 285, 404);
		scrollPaneProps.setColumnHeaderView(panelProps);
		//frame.getContentPane().add(panelProps);
		//JScrollPane propsScrollPane = new JScrollPane(table);
		
		//JPanel panelProps = new JPanel();
		//propsScrollPane.setColumnHeaderView(panelProps);
		//panel = panelProps;
	}
}
